# coding=utf-8
__author__ = 'sylar Song'
import json

import requests

from app_admin.config_interface.wxhh_config import *
from app_admin.models.model_award_pool import MAwardPool
from app_admin.models.players import Players
from app_admin.models.pay_record import MPayRecord
from app_admin.views.base_handler import BaseHandler
from . import wxhh


@wxhh.route('/get_cur_player')
class GetCurPlayer(BaseHandler):
    def get(self):
        r = requests.get(URL_GET_CUR_PLAYER)
        data = r.text
        return self.write(json.dumps({"code": 200, "data": data}))


@wxhh.route('/set_next_color')
class SetNextColor(BaseHandler):
    """
    @params 是一个dict, 用于存放花色参数 next_color
    next_color 是下一个即将开出的花色, 范围 [0,1,2,3,4]
    """

    def post(self):
        try:
            color = int(self.get_argument('next_color'))
            if color not in [0, 1, 2, 3, 4]:
                return self.write({"code": 0, "desc": "color mast in [0,1,2,3,4]"})
            params = {"next_color": color}
            r = requests.post(URL_POST_TEST_NEXT_COLOR, data=params)
            print r.text
            return self.write(r.text)
        except Exception, e:
            print e
            return self.write(json.dumps({"code": 0, "desc": "params error!!"}))


@wxhh.route('/get_next_color_list', )
class GetNextColor_list(BaseHandler):
    """
    @params 是一个dict, 用于存放花色参数 next_color
    next_color 是下一个即将开出的花色, 范围 [0,1,2,3,4]
    """

    def get(self):
        try:
            r = requests.get(URL_GET_TEST_NEXT_COLOR_LIST)
            if r.status_code == 200:
                return self.write(json.dumps({"next_color_list": r.text}))
            else:
                return self.write(json.dumps({"next_color_list": []}))
        except Exception, e:
            print e
            return self.write(json.dumps({"code": 0, "desc": "params error!!"}))


@wxhh.route('/add_gold_for_user')
class AddGoldForUser(BaseHandler):
    """
    用于通知服务器上分
    :param user: 目标用户, 实际为用户的电话号码即用户名
    :param gold: 充值金币数量
    :return:
    """

    def post(self):
        try:
            tel = self.get_argument("tel")
            gold = int(self.get_argument("gold"))
            params = {"tel": tel, "gold": gold}
            is_enough = Players.is_enough_gold(tel, gold)
            if not is_enough:
                return self.write(json.dumps({"code": 0, "desc": "not enough gold!!"}))
            r = requests.post(URL_POST_UPDATE_USER_GOLD, params)
            print r.text
            ret = json.loads(r.text)
            data = {"code": 200}
            if not ret.get("code") == 200:
                data["code"] = 603
                return self.write(json.dumps(data))
            player = Players.get_user_by_name(tel)
            if not player:
                data["code"] = 604
                return self.write(json.dumps(data))
            r = MPayRecord.create_pay_record(player.get("id"), player.get("name"), gold, pay_type=1, source_user_id=1)
            if r:
                return self.write(json.dumps(data))
            else:
                data["code"] = 605
                return self.write(json.dumps(data))
        except Exception, e:
            print e
            return self.write(json.dumps({"code": 0, "desc": "params error!!"}))


@wxhh.route('/get_all_player_list')
class GetAllPlayerList(BaseHandler):
    def get(self):
        print "get_all_player_list!!"
        page = self.get_argument('page', 1, int)
        ret = Players.get_all_user_info()
        data = {"code": 200, "ret": ret}
        return self.write(json.dumps(data))


@wxhh.route("/get_away_pool_info")
class GetAwayPoolInfo(BaseHandler):
    def get(self):
        print "get away pool info!!"
        ret = MAwardPool.get_award_info()
        data = {"code": 200, "ret": ret}
        return self.write(json.dumps(data))


@wxhh.route("/check_version")
class CheckVersion(BaseHandler):
    def get(self):
        wgt_url = "http://olbaiws2f.bkt.clouddn.com/wxhh_admin_client.wgt"
        data = {"code": 200, "ret": {"version": "1.1.0", "wgt_url": wgt_url}}
        self.write(json.dumps(data))


@wxhh.route("/get_pay_record", name="get_pay_record")
class GetPayRecord(BaseHandler):
    def get(self, *args, **kwargs):
        page = self.get_argument("page", 1)
        ret = MPayRecord.query_record_by_page(page, size=20)
        data = {"code": 200, "ret": ret}
        self.write(json.dumps(data))
